package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.PickCardEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.czy.owner.entity.PickCardEntity.CARD_TYPE_COUPON;
import static com.czy.owner.entity.PickCardEntity.CARD_TYPE_CYCLE;
import static com.czy.owner.entity.PickCardEntity.CARD_TYPE_TIMES;

/**
 * Created by King on 2017-03-15
 */

public class PickCardAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private Map<String, List<PickCardEntity>> dataMap = new HashMap<>();
    private List<String> mapKey = new ArrayList<>();

    private int cashColor = Color.parseColor("#FEA454"), disenbledColor = Color.parseColor("#EBEBEB");

    public PickCardAdapter(Context mContext, Map<String, List<PickCardEntity>> dataMap , List<String> mapKey) {
        this.mContext = mContext;
        this.dataMap = dataMap;
        this.mapKey = mapKey;
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        return dataMap.get(mapKey.get(groupPosition)).size();
    }

    @Override
    public int getGroupCount() {
        return mapKey.size();
    }

    @Override
    public List<PickCardEntity> getGroup(int groupPosition) {
        return dataMap.get(mapKey.get(groupPosition));
    }

    @Override
    public PickCardEntity getChild(int groupPosition, int childPosition) {
        return dataMap.get(mapKey.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder = null;
        if (convertView == null) {
            groupViewHolder = new GroupViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pick_coupon_header, parent, false);
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_item_coupon_title);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tvTitle.setText("预约的服务："+mapKey.get(groupPosition));
        groupViewHolder.tvTitle.setTag(groupPosition);
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        final ChildViewHolder childViewHolder;
        if (convertView == null) {
            childViewHolder = new ChildViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pick_card, parent, false);
            childViewHolder.rlContentWrapper = (RelativeLayout)convertView.findViewById(R.id.rl_item_content_wrapper);
            childViewHolder.tvCardType = (TextView) convertView.findViewById(R.id.tv_item_card_type);
            childViewHolder.tvServiceName = (TextView)convertView.findViewById(R.id.tv_item_service_name);
            childViewHolder.cbPicked = (CheckBox) convertView.findViewById(R.id.cb_item_picked);
            childViewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tv_item_price);
            childViewHolder.tvLeftTimes = (TextView) convertView.findViewById(R.id.tv_item_left_times);
            childViewHolder.tvEndTime = (TextView) convertView.findViewById(R.id.tv_item_end_time);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        final PickCardEntity mCardEntity = getChild(groupPosition,childPosition);

        switch (mCardEntity.getCardType()){
            case CARD_TYPE_TIMES:
                childViewHolder.tvCardType.setText("次卡");
                childViewHolder.rlContentWrapper.setBackgroundResource(R.drawable.background_view_rounded_time_top);
                break;
            case CARD_TYPE_CYCLE:
                childViewHolder.tvCardType.setText("周期卡");
                childViewHolder.rlContentWrapper.setBackgroundResource(R.drawable.background_view_rounded_cycle_top);
                break;
            case CARD_TYPE_COUPON:
                childViewHolder.tvCardType.setText("服务券");
                childViewHolder.rlContentWrapper.setBackgroundResource(R.drawable.background_view_rounded_coupon_top);
                break;
        }
        childViewHolder.tvServiceName.setText(mCardEntity.getCardName());
        childViewHolder.cbPicked.setTag(groupPosition + "," + childPosition);
        childViewHolder.cbPicked.setChecked(getChild(groupPosition,childPosition).isPicked());


        childViewHolder.tvPrice.setText(String.format("价值￥%.0f",mCardEntity.getPrice()));
        childViewHolder.tvLeftTimes.setText(String.format("剩余%d次",mCardEntity.getLeftCount()));


        childViewHolder.cbPicked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((CheckBox) v).isChecked()){
                    List<PickCardEntity> tmpList = dataMap.get(mapKey.get(groupPosition));
                    for (int i = 0; i < tmpList.size(); i++) {
                        tmpList.get(i).setPicked(false);
                    }
                    tmpList.get(childPosition).setPicked(true);
                }else{
                    mCardEntity.setPicked(false);
                }
                notifyDataSetChanged();
            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                childViewHolder.cbPicked.performClick();
            }
        });

        childViewHolder.tvEndTime.setText("到期时间：" + formatTime(mCardEntity.getEndTime()));
        return convertView;
    }

    public String formatTime(long timeStamp) {
        if (timeStamp == 0) {
            return "永久有效";
        }
        String formatDate = "yyyy年MM月dd日";
       SimpleDateFormat dateFormat = new SimpleDateFormat(formatDate);
        String tmpDateStr = dateFormat.format(timeStamp);
        return tmpDateStr;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    class GroupViewHolder {
        TextView tvTitle;
    }

    class ChildViewHolder {
        RelativeLayout rlContentWrapper;
        TextView tvCardType;
        TextView tvServiceName;
        CheckBox cbPicked;
        TextView tvPrice;
        TextView tvLeftTimes;
        TextView tvEndTime;
    }
}
